<template>
  <div class="edit-password-container">
    <el-card class="edit-password-card">
      <template #header>
        <div class="card-header">
          <span>修改密码</span>
        </div>
      </template>

      <el-form
        :model="passwordForm"
        :rules="rules"
        ref="formRef"
        label-width="100px"
        class="password-form"
      >
        <el-form-item label="旧密码" prop="oldPassword">
          <el-input
            v-model="passwordForm.oldPassword"
            type="password"
            placeholder="请输入旧密码"
            show-password
          ></el-input>
        </el-form-item>

        <el-form-item label="新密码" prop="newPassword">
          <el-input
            v-model="passwordForm.newPassword"
            type="password"
            placeholder="请输入新密码"
            show-password
          ></el-input>
        </el-form-item>

        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input
            v-model="passwordForm.confirmPassword"
            type="password"
            placeholder="请再次输入新密码"
            show-password
          ></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm" :loading="loading">保存</el-button>
          <el-button @click="goBack">返回</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus'
import router from '@/router'
import loginApi from '@/api/login_api.js'

const formRef = ref()
const loading = ref(false)

// 表单数据
const passwordForm = reactive({
  oldPassword: '',
  newPassword: '',
  confirmPassword: ''
})

// 表单验证规则
const rules = {
  oldPassword: [
    { required: true, message: '请输入旧密码', trigger: 'blur' },
    { min: 6, message: '密码长度至少6位', trigger: 'blur' }
  ],
  newPassword: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    { min: 6, message: '密码长度至少6位', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认新密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (value !== passwordForm.newPassword) {
          callback(new Error('两次输入的密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ]
}

// 提交表单
const submitForm = async () => {
  if (!formRef.value) return

  await formRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        // 调用修改密码的API
        const data = {
          oldPassword: passwordForm.oldPassword,
          newPassword: passwordForm.newPassword
        }
        await loginApi.changePassword(data)
        ElMessage.success('密码修改成功')
        // 清除token并跳转到登录页
        localStorage.removeItem("token")
        router.push('/login')
      } catch (error) {
        ElMessage.error('密码修改失败: ' + (error.message || '未知错误'))
      } finally {
        loading.value = false
      }
    }
  })
}

// 返回上一页
const goBack = () => {
  router.go(-1)
}
</script>

<style scoped>
.edit-password-container {
  display: flex;
  justify-content: center;
  padding: 20px;
  background-color: #fffaf5;
  min-height: 100%;
}

.edit-password-card {
  width: 100%;
  max-width: 500px;
  margin-top: 50px;
}

.card-header {
  font-size: 18px;
  font-weight: bold;
  color: #5a3e2b;
  text-align: center;
}

.password-form {
  margin-top: 20px;
}

.password-form :deep(.el-form-item__label) {
  color: #5a3e2b;
}

.password-form :deep(.el-button) {
  margin-right: 10px;
}
</style>